package com.dabaozhan.modules.wx_app.facade.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.dabaozhan.common.exception.RRException;
import com.dabaozhan.common.utils.Utils;
import com.dabaozhan.modules.wx_app.base.PageResult;
import com.dabaozhan.modules.wx_app.entity.SiteOrderEntity;
import com.dabaozhan.modules.wx_app.entity.SupplierSweepEntity;
import com.dabaozhan.modules.wx_app.entity.SupplierUserEntity;
import com.dabaozhan.modules.wx_app.entity.SupplierUserQueryEntitiy;
import com.dabaozhan.modules.wx_app.entity.UserEntity;
import com.dabaozhan.modules.wx_app.enums.ResponseCodeEnum;
import com.dabaozhan.modules.wx_app.facade.SupplierUserFacade;
import com.dabaozhan.modules.wx_app.service.SiteOrderService;
import com.dabaozhan.modules.wx_app.service.SupplierUserService;
import com.dabaozhan.modules.wx_app.service.UserService;

@Service("supplierUserFacade")
public class SupplierUserFacadeImpl implements SupplierUserFacade{

	@Autowired
	private SupplierUserService supplierUserService;

	@Autowired
	private SiteOrderService siteOrderService;

	@Autowired
	private UserService userService;

	private static final String USERTAG = "sp_";

	@Override
	public int addSupplierUser(SupplierUserEntity entity) {
		if(StringUtils.isBlank(entity.getUserId())){
			String uuid = UUID.randomUUID().toString().replace("-","");
			//如果入参没有userId就生成以sp_开头的userId
			entity.setUserId(USERTAG+uuid);
		}
		SupplierUserEntity user = supplierUserService.getSupplierUserByPhone(entity.getPhone(),entity.getPackerId());
		if(null != user){
			//该电话已经添加过供应商用户
			throw new RRException(ResponseCodeEnum.ResponseCode_105.getCode(),ResponseCodeEnum.ResponseCode_105.getDescription());
		}
		entity.setId(Utils.id().toString());
		int result = supplierUserService.addSupplierUser(entity);
		if(result > 0){
			return result;
		}else{
			//未成功插入数据，抛出业务异常 在controller里面捕获
			throw new RRException(ResponseCodeEnum.ResponseCode_102.getCode(),ResponseCodeEnum.ResponseCode_102.getDescription());
		}
	}

	@Override
	public PageResult<List<SupplierUserEntity>> getSupplierUserForPage(SupplierUserQueryEntitiy entity) {
		long count = supplierUserService.getCountForPage(entity);
		if(count <=0){
			throw new RRException(ResponseCodeEnum.ResponseCode_400.getCode(),ResponseCodeEnum.ResponseCode_400.getDescription());
		}
		//查询开始区间
		int beginIndex = (entity.getPage()-1)*entity.getSize();
		entity.setBeginIndex(beginIndex);
		//查询结束区间
		int endIndex = entity.getPage()*entity.getSize();
		entity.setEndIndex(endIndex);
		List<SupplierUserEntity> list = supplierUserService.getSupplierUserForPage(entity);
		if(CollectionUtils.isEmpty(list)){
			throw new RRException(ResponseCodeEnum.ResponseCode_400.getCode(),ResponseCodeEnum.ResponseCode_400.getDescription());
		}
		for(SupplierUserEntity user:list){
			//默认设置为0
			user.setAllOrderPrice(BigDecimal.ZERO);
			user.setAllOrderNetWeigth(BigDecimal.ZERO);
			List<SiteOrderEntity> orderList = siteOrderService.getSiteOrderBySupplierUserIdAndPackerId(user.getUserId(),entity.getPackerId());
			if(!CollectionUtils.isEmpty(orderList)){
				BigDecimal totalPrice = new BigDecimal(0);
				BigDecimal totalWeight = new BigDecimal(0);
				for(SiteOrderEntity order:orderList){
					totalPrice = totalPrice.add(order.getMoneyTotal());
					totalWeight = totalWeight.add(order.getNetWeight());
				}
				user.setAllOrderPrice(totalPrice);
				user.setAllOrderNetWeigth(totalWeight);
			}
		}
		PageResult<List<SupplierUserEntity>> pageResult = new PageResult<List<SupplierUserEntity>>();
		pageResult.setTotal(count);
		pageResult.setListData(list);
		return pageResult;
	}

	@Override
	public int deleteSupplierUser(String userId,String packerId) {
		int result = supplierUserService.deleteSupplierUser(userId,packerId);
		if(result > 0){
			return result;
		}else{
			//未成功插入数据，抛出业务异常 在controller里面捕获
			throw new RRException(ResponseCodeEnum.ResponseCode_106.getCode(),ResponseCodeEnum.ResponseCode_106.getDescription());
		}
	}

	@Override
	public int updateSupplierUser(SupplierUserEntity supplierUser) {
		int result = supplierUserService.updateSupplierUser(supplierUser);
		if(result > 0){
			return result;
		}else{
			//未成功更新数据，抛出业务异常 在controller里面捕获
			throw new RRException(ResponseCodeEnum.ResponseCode_107.getCode(),ResponseCodeEnum.ResponseCode_107.getDescription());
		}
	}

	@Override
	public SupplierUserEntity sweepSupplier(SupplierSweepEntity entity) {
		SupplierUserEntity resultSupplier = new SupplierUserEntity();
		UserEntity userEntity = userService.findByCardUserId(entity.getId());
		if(null == userEntity){
			throw new RRException(ResponseCodeEnum.ResponseCode_110.getCode(),ResponseCodeEnum.ResponseCode_110.getDescription());
		}
		if(StringUtils.isBlank(userEntity.getTelephone())){
			throw new RRException(ResponseCodeEnum.ResponseCode_109.getCode(),ResponseCodeEnum.ResponseCode_109.getDescription());
		}
		if(StringUtils.isBlank(userEntity.getContacts())){
			throw new RRException(ResponseCodeEnum.ResponseCode_111.getCode(),ResponseCodeEnum.ResponseCode_111.getDescription());
		}
		SupplierUserEntity supplierUser = supplierUserService.getSupplierUserByPhone(userEntity.getTelephone(),entity.getPackerId());
		//是供应商用户 直接返回
		if(null != supplierUser){
			resultSupplier.setPhone(supplierUser.getPhone());
			resultSupplier.setUserId(supplierUser.getUserId());
			resultSupplier.setSweepType(1);
			resultSupplier.setName(supplierUser.getName());
			resultSupplier.setId(supplierUser.getId());
			return resultSupplier;
		}
		//不是供应商用户 组装实体 并添加
		SupplierUserEntity addEntity = new SupplierUserEntity();
		String addId = Utils.id().toString();
		addEntity.setId(addId);
		addEntity.setUserId(entity.getId());
		addEntity.setPhone(userEntity.getTelephone());
		addEntity.setName(userEntity.getContacts());
		addEntity.setPackerId(entity.getPackerId());
		addEntity.setIdNumber(userEntity.getCardNum());
		int result = supplierUserService.addSupplierUser(addEntity);
		if(result > 0){
			resultSupplier.setPhone(userEntity.getTelephone());
			resultSupplier.setUserId(entity.getId());
			resultSupplier.setName(userEntity.getContacts());
			resultSupplier.setSweepType(2);
			resultSupplier.setId(addId);
			return resultSupplier;
		}else{
			//未成功插入数据，抛出业务异常 在controller里面捕获
			throw new RRException(ResponseCodeEnum.ResponseCode_102.getCode(),ResponseCodeEnum.ResponseCode_102.getDescription());
		}

	}

}
